﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Common.js" type="text/javascript"></script>
    <script type="text/javascript">

        function Test() {
            alert("test");
        }
        //        if (window.attachEvent) {
        //            // ie
        //            document.getElementById("btn").attachEvent("onclick", Test);
        //        }
        //        else {
        //            // ff
        //            document.getElementById("btn").addEventLinstener("click", Test, false);
        //        }


        BindEvent(window, "load", function (e) {

            BindEvent(document.getElementById("btn"), "click", Test);
            BindEvent("btn", "click", Test);

            BindEvent("txtName", "blur", TextBlur);
            BindEvent("txtPwd", "blur", TextBlur);
            BindEvent("areRemark", "keydown", function (e) {
                //Get("btn").click();
            });
            BindEvent("txtPwd", "keydown", function (e) {
                var code = GetCode(e);
                if (code < 47 || code > 58) {
                    CancelEvent(e);
                    // alert("55");
                }
            });

            BindEvent("divMath", "click", function (e) {
                Get('txtSubject').value = '数学';
                CancelBubblie(e);
            });
            BindEvent("divEnglish", "click", function (e) {
                Get('txtSubject').value = '英语';
                CancelBubblie(e);
            });
            BindEvent("divAll", "click", function (e) { Get('txtSubject').value = '全部'; });
            BindEvent("txtSubject", "change", function (e) {
                //CancelEvent(e);
                setTimeout("AT();", 0);

                //alert("test");
            });


        });

        function AT() {
            Get('txtSubject').focus();
            //alert("ttt");
        }

        function CancelBubblie(e) {
            if (window.event) {
                //ie
                window.event.cancelBubble = true;
            }
            else {
                //ff w3c
                e.stopPropagation();
            }
        }

        // 取消事件
        function CancelEvent(e) {
            if (window.event) {
                //ie
                window.event.returnValue = false;
            }
            else {
                //ff w3c
                e.preventDefault();
            }
        }

        // 得到输入
        function GetCode(e) {
            var code = null;
            if (window.event) {
                code = window.event.keyCode;
            }
            else {
                code = e.which;
            }
            return code;
        }

        // 获得事件源对象
        function GetEventSrcElement(e) {
            if (window.event) {
                //ie
                return window.event.srcElement
            }
            // ff
            return e.target
        }

        // 失去焦点 判断长度 > 4 长度<6
        function TextBlur(e) {
            var txt = GetEventSrcElement(e);

            if ((txt.value.length < 4 || txt.value.length > 6) && txt.value.length != 0) {
                txt.focus();

                alert("长度不符!");
            }
        }
    
    </script>
</head>
<body>
    <input id="btn" type="button" onclick="alert('test')" value="test" />
    <input id="txtName" type="text" />
    <input id="txtPwd" type="text" />
    <textarea id="areRemark" rows="5" cols="20"></textarea>
    <input id="txtSubject" type="text" />
    <div id="divAll" style="background-color: Red; width: 100px; height: 100px;">
        <div id="divMath" style="background-color: Yellow; width: 50px; height: 50px; float: left;">
            数学</div>
        <div id="divEnglish" style="background-color: Green; width: 50px; height: 50px; float: left;">
            英语</div>
        全部
    </div>
</body>
</html>
